package hu.elte.inf.holiuml.coordgeom;

/**
 * Represents a line segment on the plane.
 * 
 * It is an immutable object.
 * 
 * @author Csaba Hoch
 */
public class LineSegment {

	private CGPoint p1;
	private CGPoint p2;
	private Line l1;
	private Line l2;
	private Line l3;

	public LineSegment(CGPoint p1, CGPoint p2) {
		this.p1 = p1;
		this.p2 = p2;
		CGVector v = (new CGVector(p1, p2)).getRotatedPositive();
		l1 = new Line(p1, p2);
		l2 = new Line(p1, v);
		l3 = new Line(p2, v);
	}

	public void draw(java.awt.Graphics g) {
		g.drawLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
	}

	public boolean near(CGPoint p) {
		// System.out.println(l2.isOnTheRight(p));
		// System.out.println(l3.isOnTheLeft(p));
//		System.out.println("near:" + l1.isNear(p));
//		System.out.println("left:" + l1.isOnTheLeft(p));
//		System.out.println("p1: " + p1.getX()+","+p1.getY());
//		System.out.println("p2: " + p2.getX()+","+p2.getY());
//		System.out.println("eq: a=" + l1.getA() + ", b=" + l1.getB() + ", c="
//				+ l1.getC());

		return l2.isOnTheRight(p) && l3.isOnTheLeft(p) && l1.isNear(p);

	}

	public CGPoint getBeginPoint() {
		return p1;
	}
	
	public CGPoint getEndPoint() {
		return p2;
	}
	
	public CGPoint getCenterPoint() {
		return new CGPoint(((p1.getX() + p2.getX()) / 2), ((p1.getY() + p2
				.getY()) / 2));
	}

	public Line getLine() {
		return l1;
	}
}
